package com.sdyeda.dtu.net;

import java.util.Arrays;

import com.sdyeda.tool.HexTools;

public class IotInfo {
	private String id;
	private String type;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public static IotInfo getIotInfo(byte[] buff) {
		IotInfo iotinfo = new IotInfo();
		//
		int seek = findHead(buff);
		// System.out.println("seek="+seek);
		// System.out.println(buff.length);
		if (seek >= 0 && buff.length >= seek + 6 + 8) {
			iotinfo.setId(HexTools.bytesToHexString(buff, seek + 6, 8));
			if (buff.length == 60+seek) {
				byte[] strtype = new byte[8];
				System.arraycopy(buff, seek+6+45, strtype, 0, 8);
				
				try {
					iotinfo.setType(new String(strtype, "GB2312").trim());
				} catch (Exception e) {

				}
				//)
			}
			return iotinfo;
		} else {
			return null;
		}
		// iotinfo.setType();

	}

	static int findHead(byte[] buff) {
		byte[] head = { (byte) 0x55, 0x7A };

		byte[] temp = new byte[2];
		for (int i = 0; i < buff.length - 2; i++) {
			System.arraycopy(buff, i, temp, 0, 2);
			if (Arrays.equals(temp, head)) {
				return i;
			}
		}
		return -1;
	}

}
